/*==================================================================
Project: 	INCREASING THE DEMAND FOR WORKERS WITH A CRIMINAL RECORD
Info: 		Creates appendix figures for paper.
Program: 	3_appendixfigures.do
Created: 	June 28, 2022
Edited:  	June 29, 2022 by KG
Purpose: 	Creates appendix figures for paper on workers with a criminal conviction at the Platform.
Note: 		Set the working directory to the Replication folder that contains this do file.
==================================================================*/
cap log close
clear all
version 16, permanently
discard

***** Set paths.
global Main "`c(pwd)'"
cd "$Main"

* Data directory
global Data "$Main/analysis_data"

* Figures directory 
global Figures "$Main/output_figures"

* Install packages
ssc install ftools, replace
ssc install ivreg2, replace
ssc install reghdfe, replace
ssc install grstyle, replace
ssc install palettes, replace
ssc install colrspace, replace
ssc install gtools, replace
ssc install blindschemes, replace
ssc install ipfweight, replace
ssc install ranktest, replace
ssc install sencode, replace
net install allston, from("https://raw.githubusercontent.com/dballaelliott/allston/master/") replace

* set graph style
graph set window fontface "Times New Roman"
set scheme s2mono
grstyle init platform, replace 
grstyle clockdir subtitle_position 11
grstyle gridringstyle subtitle_ring 0
grstyle set legend 7, inside nobox
grstyle gsize text medium     
grstyle gsize text_option medium     
grstyle gsize filled_text medium     
grstyle gsize key_label medium    
grstyle gsize label medium    
* turn off grid lines
grstyle yesno draw_major_hgrid       no
grstyle yesno draw_major_vgrid       no
gr_setscheme, scheme(platform)

/*==================================================================
*** RUN A LOG FILE ***
==================================================================*/
global date = "$S_DATE"
global LogPath = "$Main/log"
capture log close
log using "$LogPath/WCC_$date.log", append

/*==================================================================
*** LOAD PROGRAMS ***
==================================================================*/

/***
Program: residualize
Purpose: Calculate residuals
***/
program define residualize
/****************************************************************************** 
required: varname varlist
first variable = variable to residualize
second variable (or varlist) = variable that we are residualizing on 
(i.e. pulling out)
also required: 
either replace or [gen]erate
generate puts the residualized version in a new variable
replace replaces varname with the residualized version
******************************************************************************/
syntax varlist(min=1 ts fv), [replace] [GENerate(name) absorb(passthru)]

if missing("`absorb'") local absorb noabsorb 

local y: word 1 of `varlist' 

reghdfe `varlist', `absorb' resid 

su `y' if e(sample), meanonly 
local mu = `r(mean)'

tempvar residual 
predict `residual', residual 

if !missing("`replace'") replace `y' = `residual' + `mu' 
else if !missing("`generate'") gen `generate' = `residual' + `mu' 
else di as error "Please specificy " as input "replace" as error " or " as input "generate({it:varname})" 

end 

/***
Program: store_estimates
Purpose: Stores beta, se, and p-value estimates.
***/
program define store_estimates, rclass

args var

if "`var'" == "lincom"{
    local b = string(r(estimate), "%10.3fc") 
    local se = "(" + string(r(se), "%10.3fc") + ")"
}
else{
    local b = string(_b[`var'], "%10.3fc") 
    local se = "(" + string(_se[`var'], "%10.3fc") + ")"
}

local p = 2*(1-normal(abs(`b'/`se')))

local star=""
if `p' >.05 & `p' <=.10 {   
    local star = "^{*}"
}
if `p' >.01 & `p' <=.05 {   
    local star = "^{**}"
}
if `p' <=.01 {  
    local star = "^{***}" 
}
local p_val=string(`p', "%10.3fc")

if `se' == `b' & `se' == 0 { 
    local b "\multicolumn{1}{c}{--}" 
    local se "" 
}

return local beta = "`b'"
return local se = "`se'"
/* if !missing("`star'") return local star = "$`star'$" */
return local pvalue= "`p_val'"

end

/*==================================================================
*** MAKE FIGURES ***
==================================================================*/

/*===========================================================================
Appendix Figure B.1: Labor Demand Curve
=============================================================================*/

* Estimate baseline elasticity 
use "$Data/main_survey_wide.dta", clear
su effective_wage, meanonly 
local avg_w = r(mean)
su hire_sub, meanonly
local avg_H = r(mean)
local R = `avg_w'/`avg_H'
reg hire_sub effective_wage,  cluster(firm_id)
lincom `R'*_b[effective_wage]
store_estimates "lincom" 
local elast_base `r(beta)'`r(star)'
local se_elast_base `r(se)'

* Normalize outcome variable
replace hire_sub = 100 * hire_sub 

* Generate matrix that stores the regression results
local row=1
mat R=J(5,4,.)

* Full sample regression results
reg hire_sub ibn.subsidy_rate, nocons cluster(firm_id)
mat li r(table)
foreach X in 0bn.subsidy_rate 10.subsidy_rate 25.subsidy_rate 50.subsidy_rate 100.subsidy_rate {
    mat R[`row',1] = _b[`X']
    mat R[`row',2] = _b[`X']-1.96*_se[`X']
    mat R[`row',3] = _b[`X']+1.96*_se[`X']
    mat R[`row',4] = `row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "Baseline" 2 "10%" 3 "25%" 4 "50%" 5 "100%"
label values R4 R4

* Add x axis spacing between groups
generate x_value = 100 if R4 == 1
replace x_value = 90 if R4 == 2
replace x_value = 75 if R4 == 3
replace x_value = 50 if R4 == 4
replace x_value = 0 if R4 == 5
generate pos = 3
replace pos = 2 if x_value == 90
replace pos = 3 if x_value == 0

* Generate graph
gen t_hire = round(R1)
twoway (scatter R1 x_value, mlab(t_hire) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium)) legend(off) ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2)) /// 
text(10 25 "Elasticity, {&epsilon}{sup:D} = `elast_base' `se_elast_base'", size(medium))) ///
(lfit R1 x_value) ///
(rcap R3 R2 x_value, lc(gs5))


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf1_demand_curve.pdf", replace  

/*===========================================================================
Appendix Figure B.2: Labor Market Conditions 
=============================================================================*/

/*------------------  Panel A: No Subsidy Sample ------------------*/
use "$Data/main_survey_wide.dta", clear

* Normalize outcome variable
replace hire_sub = 100 * hire_sub 

local row=1
mat R=J(10,5,.)

* Estimates by hypothetical unemployment rate shown 
reg hire_sub ibn.ue_rate if subsidy_rate == 0, nocons cluster(firm_id) 
foreach X in 10.ue_rate 2.ue_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-value of respondents shown 2% hypothetical unemployment relative to those shown 10% 
reg hire_sub ib(10).ue_rate if subsidy_rate == 0, cluster(firm_id)
local t = _b[2.ue_rate]/_se[2.ue_rate]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_1 = substr(string(`p', "%8.2f"), 2, .)

* Estimates by metropolitan area actual unemployment rate in January-March 2020
su met_ue_rate, det 
gen high_ue = met_ue_rate > r(p50) if !missing(met_ue_rate) & subsidy_rate == 0
gen low_ue = high_ue == 0 & !missing(high_ue)
reg hire_sub high_ue low_ue if subsidy_rate == 0, nocons cluster(firm_id)
foreach X in high_ue low_ue {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-value of high metropolitan unemployment rate relative to low
preserve
reg hire_sub high_ue if subsidy_rate == 0, cluster(firm_id)
local t = _b[high_ue]/_se[high_ue]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_2 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimates by industry unemployment rate
su local_ind_ue_rate, det 
gen high_ue_2 = local_ind_ue_rate > r(p50) if !missing(local_ind_ue_rate) & subsidy_rate == 0
gen low_ue_2 = high_ue_2 == 0 & !missing(high_ue_2)
reg hire_sub high_ue_2 low_ue_2 if subsidy_rate == 0, nocons cluster(firm_id)
foreach X in high_ue_2 low_ue_2 {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-values for high vs. low industry unemployment rate
preserve
reg hire_sub high_ue_2 if subsidy_rate == 0, cluster(firm_id)
local t = _b[high_ue_2]/_se[high_ue_2]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_3 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimates by industry unemployment for people with high school or below education level
su local_low_skill_ue_rate, det 
gen high_ue_3 = local_low_skill_ue_rate > r(p50) if !missing(local_low_skill_ue_rate) & subsidy_rate == 0
gen low_ue_3 = high_ue_3 == 0 & !missing(high_ue_3)
reg hire_sub high_ue_3 low_ue_3  if subsidy_rate == 0, nocons cluster(firm_id)
foreach X in high_ue_3 low_ue_3 {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-value for high vs. low industry unemployment for low skill labor
preserve
reg hire_sub high_ue_3  if subsidy_rate == 0, cluster(firm_id)
local t = _b[high_ue_3]/_se[high_ue_3]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_4 = substr(string(`p', "%8.2f"), 2, .)
restore

clear
svmat R
la var R1 "Willingness to Work with WCs (%)"

* Define group based on order of regressions and predictions above
gen Group = .
replace Group = 1 if R4 == 1 | R4 == 2
replace Group = 2 if R4 == 3 | R4 == 4
replace Group = 3 if R4 == 5 | R4 == 6
replace Group = 4 if R4 == 7 | R4 == 8

label define Group 1 "Potential Local Unemployment"  2 "Metro-Level Unemployment"  3 "Metro-Level Industry Unemployment"  4 "HS or Below Unemployment"
label values Group Group

* Add x axis spacing between groups
generate x_value = R4 if Group == 1
replace x_value = R4 + 0.5 if Group == 2
replace x_value = R4 + 1 if Group == 3
replace x_value = R4 + 1.5 if Group == 4
sort x_value 
list x_value

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1  `""10%" "Potential" "Unemployment""'  2 `""2%" "Potential" "Unemployment""' 3.5 `""Above Median" "Unemployment""' 4.5  `""Below Median" "Unemployment""' 6  `""Above Median" "Metro Ind." "Unemployment""'  7  `""Below Median" "Metro Ind." "Unemployment""'  8.5 `""Above Median" "Low Skill" "Unemployment""' 9.5`""Below Median" "Low Skill" "Unemployment""' , labsize(medlarge)) ///
ylabel(0(020)100, nogrid angle(0) labsize(medlarge)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%", size(medlarge)) text(20 2 "`coef_2'%", size(medlarge)) text(20 3.5 "`coef_3'%", size(medlarge))  text(20 4.5 "`coef_4'%", size(medlarge)) text(15 6 "`coef_5'%", size(medlarge)) text(19 7 "`coef_6'%", size(medlarge))  text(20 8.5 "`coef_7'%", size(medlarge))  text(20 9.5 "`coef_8'%", size(medlarge)) ///
xline(2.75, lpattern(-) lcolor(black)) ///
xline(5.25, lpattern(-) lcolor(black)) ///
xline(7.75, lpattern(-) lcolor(black)) ///
text(95 1.5 "{it:p}-value=`p_value_1'", size(medlarge)) text(95 4 "{it:p}-value=`p_value_2'", size(medlarge)) text(95 6.5 "{it:p}-value=`p_value_3'", size(medlarge)) text(95 9 "{it:p}-value=`p_value_4'", size(medlarge)) ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)", size(medlarge)) xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 

graph display, ysize(3) xsize(9)
graph export "$Figures/xf2a_labormarket_nosub.pdf", replace 

/*------------------  Panel B: Full Sample ------------------*/
use "$Data/main_survey_wide.dta", clear

* Normalize outcome variable
replace hire_sub = 100 * hire_sub 

local row=1
mat R=J(10,5,.)

* Estimates by hypothetical unemployment rate shown 
reg hire_sub ibn.ue_rate i.subsidy_rate, nocons cluster(firm_id) 
foreach X in 10.ue_rate 2.ue_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'	
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-value of respondents shown 2% hypothetical unemployment relative to those shown 10% 
reg hire_sub ib(10).ue_rate i.subsidy_rate, cluster(firm_id)
local t = _b[2.ue_rate]/_se[2.ue_rate]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_1 = substr(string(`p', "%8.2f"), 2, .)

* Estimates by metropolitan area actual unemployment rate in January-March 2020
su met_ue_rate, det 
gen high_ue = met_ue_rate > r(p50) if !missing(met_ue_rate) 
gen low_ue = high_ue == 0 & !missing(high_ue)
reg hire_sub high_ue low_ue i.subsidy_rate, nocons cluster(firm_id)
foreach X in high_ue low_ue {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-value of high metropolitan unemployment rate relative to low
preserve
reg hire_sub high_ue i.subsidy_rate, cluster(firm_id)
local t = _b[high_ue]/_se[high_ue]
local p =2*ttail(e(df_r),abs(`t'))
local p_value_2 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimates by industry unemployment rate
su local_ind_ue_rate, det 
gen high_ue_2 = local_ind_ue_rate > r(p50) if !missing(local_ind_ue_rate)
gen low_ue_2 = high_ue_2 == 0 & !missing(high_ue_2)
reg hire_sub high_ue_2 low_ue_2 i.subsidy_rate, nocons cluster(firm_id)
foreach X in high_ue_2 low_ue_2 {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-value for high vs. low industry unemployment rate
preserve
reg hire_sub high_ue_2 i.subsidy_rate, cluster(firm_id)
local t = _b[high_ue_2]/_se[high_ue_2]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_3 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimates by industry unemployment for people with high school or below education level
su local_low_skill_ue_rate, det 
gen high_ue_3 = local_low_skill_ue_rate > r(p50) if !missing(local_low_skill_ue_rate)
gen low_ue_3 = high_ue_3 == 0 & !missing(high_ue_3)
reg hire_sub high_ue_3 low_ue_3 i.subsidy_rate, nocons cluster(firm_id)
foreach X in high_ue_3 low_ue_3 {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-value for high vs. low industry unemployment for low skill labor
preserve
reg hire_sub high_ue_3 i.subsidy_rate, cluster(firm_id)
local t = _b[high_ue_3]/_se[high_ue_3]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_4 = substr(string(`p', "%8.2f"), 2, .)
restore

clear
svmat R
la var R1 "Willingness to Work with WCs (%)"

* Define group based on order of regressions and predictions above
gen Group = .
replace Group = 1 if R4 == 1 | R4 == 2
replace Group = 2 if R4 == 3 | R4 == 4
replace Group = 3 if R4 == 5 | R4 == 6
replace Group = 4 if R4 == 7 | R4 == 8

label define Group 1 "Potential Local Unemployment"  2 "Metro-Level Unemployment"  3 "Metro-Level Industry Unemployment"  4 "HS or Below Unemployment"
label values Group Group

* Add x axis spacing between groups
generate x_value = R4 if Group == 1
replace x_value = R4 + 0.5 if Group == 2
replace x_value = R4 + 1 if Group == 3
replace x_value = R4 + 1.5 if Group == 4
sort x_value 
list x_value

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1  `""10%" "Potential" "Unemployment""'  2 `""2%" "Potential" "Unemployment""' 3.5 `""Above Median" "Unemployment""' 4.5  `""Below Median" "Unemployment""' 6  `""Above Median" "Metro Ind." "Unemployment""'  7  `""Below Median" "Metro Ind." "Unemployment""'  8.5 `""Above Median" "Low Skill" "Unemployment""' 9.5`""Below Median" "Low Skill" "Unemployment""' , labsize(medlarge)) ///
ylabel(0(020)100, nogrid angle(0) labsize(medlarge)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%", size(medlarge)) text(20 2 "`coef_2'%", size(medlarge)) text(20 3.5 "`coef_3'%", size(medlarge))  text(20 4.5 "`coef_4'%", size(medlarge)) text(15 6 "`coef_5'%", size(medlarge)) text(19 7 "`coef_6'%", size(medlarge))  text(20 8.5 "`coef_7'%", size(medlarge))  text(20 9.5 "`coef_8'%", size(medlarge)) ///
xline(2.75, lpattern(-) lcolor(black)) ///
xline(5.25, lpattern(-) lcolor(black)) ///
xline(7.75, lpattern(-) lcolor(black)) ///
text(95 1.5 "{it:p}-value=`p_value_1'", size(medlarge)) text(95 4 "{it:p}-value=`p_value_2'", size(medlarge)) text(95 6.5 "{it:p}-value=`p_value_3'", size(medlarge)) text(95 9 "{it:p}-value=`p_value_4'", size(medlarge)) ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)", size(medlarge)) xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 

graph display, ysize(3) xsize(9)
graph export "$Figures/xf2b_labormarket_full.pdf", replace 

/*===========================================================================
Appendix Figure B.3: Labor Demand Curve for Different Treatments
=============================================================================*/

* Estimate baseline elasticities
use "$Data/main_survey_long.dta", clear
su effective_wage, meanonly 
local avg_w = r(mean)
su hire_sub, meanonly
local avg_H = r(mean)
local R = `avg_w'/`avg_H'
reg hire_sub effective_wage,  cluster(firm_id)
lincom `R'*_b[effective_wage]
store_estimates "lincom" 
local elast_base `r(beta)'`r(star)'
di `elast_base'
local se_elast_base `r(se)'


* Insurance elasticity 
use "$Data/main_survey_long.dta", clear
local rates "0 10 25 50 100"
local ins "5000 100000 5000000"
foreach i in `ins'{
    tempvar avg_w avg_H R_var
    egen  `avg_w' = mean(effective_wage)
    egen  `avg_H' = mean(hire_ins)
    g     `R_var' = `avg_w'/`avg_H'
    *mkmat `R_var', matrix(R_mat)
    *local R R_mat[1,1]
    local R = `R_var'[1]
    reg hire_ins effective_wage if ins_cap == `i',  cluster(firm_id)
    lincom `R'*_b[effective_wage]
    store_estimates "lincom" 
    local elast_ins`i' `r(beta)'`r(star)'
    di `elast_ins`i''
    local se_elast_ins`i' `r(se)'
    di `se_elast_ins`i''
}

* Perf History elasticity 
use "$Data/main_survey_long.dta", clear
local past "1 5 25"
foreach i in `past'{
    tempvar avg_w avg_H R_var
    egen  `avg_w' = mean(effective_wage)
    egen  `avg_H' = mean(hire_hist)
    g     `R_var' = `avg_w'/`avg_H'
    *mkmat `R_var', matrix(R_mat)
    *local R R_mat[1,1]
    local R = `R_var'[1]
    reg hire_hist effective_wage if past_jobs == `i',  cluster(firm_id)
    lincom `R'*_b[effective_wage]
    store_estimates "lincom" 
    local elast_past`i' `r(beta)'`r(star)'
    local se_elast_past`i' `r(se)'
}

* Mean Effects - Insurance
use "$Data/main_survey_long.dta", clear
reg hire_ins   i.subsidy_rate i.ins_cap,   cluster(firm_id)
foreach i of local ins {
    store_estimates `i'.ins_cap
    local mean_ins`i' `r(beta)'`r(star)'
    local se_mean_ins`i' `r(se)'
    lincom  `i'.ins_cap - 5000.ins_cap
    store_estimates "lincom"
    local b_mean_ins`i' 	`r(beta)'`r(star)'
    local se_b_mean_ins`i'	`r(se)'
} 

* Mean Effects - Job History 
use "$Data/main_survey_long.dta", clear
reg hire_hist  i.subsidy_rate i.past_jobs, cluster(firm_id)
foreach j of local past {
    store_estimates `j'.past_jobs
    local mean_past`j' `r(beta)'`r(star)'
    local se_mean_past`j' `r(se)'
    lincom  `j'.past_jobs - 1.past_jobs
    store_estimates "lincom"
    local b_mean_past`j' 	`r(beta)'`r(star)'
    local se_b_mean_past`j'	`r(se)'
} 

* Years Since Conviction Elasticity
use "$Data/main_survey_long.dta", clear
local years 1 3 7 
tempvar avg_w avg_H R_var
egen  `avg_w' = mean(effective_wage)
egen  `avg_H' = mean(hire_clean)
g     `R_var' = `avg_w'/`avg_H'
*mkmat `R_var', matrix(R_mat)
*local R R_mat[1,1]
local R = `R_var'[1]
foreach i in `years' {
    reg hire_clean effective_wage if years_elapsed == `i'
    lincom `R'*effective_wage
    store_estimates "lincom"
    local elast_yrs`i' 		`r(beta)'`r(star)'
    local se_elast_yrs`i' 	`r(se)'
}

* Mean Effects - Performance History
use "$Data/main_survey_long.dta", clear
reg hire_clean i.subsidy_rate i.years_elapsed, cluster(firm_id)
foreach i of local years {
    store_estimates `i'.years_elapsed
    local mean_yrs`i' 		`r(beta)'`r(star)'
    local se_mean_yrs`i'	`r(se)'
    lincom  `i'.years_elapsed - 1.years_elapsed
    store_estimates "lincom"
    local b_mean_yrs`i' 	`r(beta)'`r(star)'
    local se_b_mean_yrs`i'	`r(se)'
}

use "$Data/main_survey_long.dta", clear

* Normalize outcome variables
foreach var in hire_sub hire_ins hire_hist hire_unemp hire_clean { 
    replace `var'= 100*`var'
} 

* Generate matrix that stores the regression results
local row=1
mat R=J(20,5,.)

reg hire_sub ibn.subsidy_rate, nocons cluster(firm_id)
mat li r(table)
di 
foreach X in 0bn.subsidy_rate 10.subsidy_rate 25.subsidy_rate 50.subsidy_rate 100.subsidy_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=1
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

*Insurance Results
reg hire_ins ib5000.ins_cap ibn.subsidy_rate, cluster(firm_id) noconstant coefl
foreach X in 0bn.subsidy_rate 10.subsidy_rate 25.subsidy_rate 50.subsidy_rate 100.subsidy_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=2
    local coef_2 = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* Past Jobs Results
reg hire_hist ib1.past_jobs ibn.subsidy_rate, cluster(firm_id) noconstant coefl
foreach X in 0bn.subsidy_rate 10.subsidy_rate 25.subsidy_rate 50.subsidy_rate 100.subsidy_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=3
    local coef_2 = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* Limited Screening Results
reg hire_clean ib1.years_elapsed ibn.subsidy_rate, cluster(firm_id) noconstant coefl
foreach X in 0bn.subsidy_rate 10.subsidy_rate 25.subsidy_rate 50.subsidy_rate 100.subsidy_rate {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=4
    local coef_2 = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"

* Add x axis spacing between groups
generate x_value = 100 if inlist(R4, 1, 6, 11, 16)
replace x_value = 90 if inlist(R4, 2, 7, 12, 17)
replace x_value = 75 if inlist(R4, 3, 8, 13, 18)
replace x_value = 50 if inlist(R4, 4, 9, 14, 19)
replace x_value = 0 if inlist(R4, 5, 10, 15, 20)
generate pos = 3
replace pos = 2 if R4 == 2
replace pos = 2 if R4==15 | R4 == 17 | R4 == 20 | R4 == 18
gen t_hire = round(R1)
gen x_value_1 = x_value - 1
gen x_value_2 = x_value + 1

* Graph insurance results
twoway (scatter R1 x_value_1 if R5 == 1, mlab(t_hire) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium)) ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2))) ///
(lfit R1 x_value if R5 == 1) ///
(lfit R1 x_value if R5 == 2) ///
(scatter R1 x_value_2 if R5 == 2, mlab(t_hire) mcolor(gs70) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium))  ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2)) legend(rows(3) order(2 "Baseline, {&epsilon}{sup:D} = `elast_base' `se_elast_base'" 3 "$5k Insurance Cap, {&epsilon}{sup:D} = `elast_ins5000' `se_elast_ins5000'" - "Mean Effect = `mean_ins5000' `se_mean_ins5000'"))) ///
(rcap R3 R2 x_value_1  if R5 == 1, lc(gs5)) ///
(rcap R3 R2 x_value_2  if R5 == 2, lc(gs5))

graph display, ysize(5) xsize(6)	
graph export "$Figures/xf3a_insurance_demand_curve.pdf", replace  


* Graph job history results
twoway (scatter R1 x_value_1 if R5 == 1, mlab(t_hire) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium)) ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2))) ///
(lfit R1 x_value if R5 == 1) ///
(lfit R1 x_value if R5 == 3) ///
(scatter R1 x_value_2 if R5 == 3, mlab(t_hire) mcolor(gs70) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium))  ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2)) legend(rows(3) order(2 "Baseline. {&epsilon}{sup:D} = `elast_base' `se_elast_base'" 3 "1 Past Job. {&epsilon}{sup:D} = `elast_past1' `se_elast_past1'" - "Mean Effect = `mean_past1' `se_mean_past1'"))) ///
(rcap R3 R2 x_value_1  if R5 == 1, lc(gs5)) ///
(rcap R3 R2 x_value_2  if R5 == 3, lc(gs5))

graph display, ysize(5) xsize(6)	
graph export "$Figures/xf3b_jobhistory_demand_curve.pdf", replace  


* Graph limited record screening results
twoway (scatter R1 x_value_1 if R5 == 1, mlab(t_hire) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium)) ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2))) ///
(lfit R1 x_value if R5 == 1) ///
(lfit R1 x_value if R5 == 4) ///
(scatter R1 x_value_2 if R5 == 4, mlab(t_hire) mcolor(gs70) mlabsize(medium) mlabgap(*1) mlabvpos(pos) graphregion(color(white)) bgcolor(white) ///
title("", size(medium))  ///
xlabel(0 "100%" 50 "50%" 75 "75%" 90 "25%" 100 "0%") ylabel(0(20)100, nogrid angle(0))   /// 
ytitle("Willing to Work with WCs (%)") xtitle("Wage Subsidy",size(medium)) subtitle("",  margin(zero) size(medium) pos(11)) plotregion(margin(l+2)) legend(rows(3) order(2 "Baseline, {&epsilon}{sup:D} = `elast_base' `se_elast_base'" 3 "1 Year Clean, {&epsilon}{sup:D} = `elast_yrs1' `se_elast_yrs1'" -  "Mean Effect = `mean_yrs1' `se_mean_yrs1'"))) ///
(rcap R3 R2 x_value_1  if R5 == 1, lc(gs5)) ///
(rcap R3 R2 x_value_2  if R5 == 4, lc(gs5))

graph display, ysize(5) xsize(6)	
graph export "$Figures/xf3c_limitedscreening_demand_curve.pdf", replace  

/*===========================================================================
Appendix Figure B.4: Crime and Safety Insurance Levels 
=============================================================================*/

/*------------------  Panel A: No Subsidy Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_ins = 100 * hire_ins

local row=1
mat R=J(10,5,.)

* Estimates by insurance cap level
reg hire_ins ibn.ins_cap if subsidy_rate==0, cluster(firm_id) noconstant
foreach X in 5000.ins_cap 100000.ins_cap 5000000.ins_cap {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=1
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-values by insurance cap level relative to $5000 insurance cap result
local row=2
foreach X in 100000.ins_cap 5000000.ins_cap {
    preserve
    reg hire_ins ib5000.ins_cap if subsidy_rate==0, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p = 2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "$5,000 Insurance" 2 "$100,00 Insurance" 3 "$5 Million Insurance"
label values R4 R4

* Add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  `""$5,000" "Insurance""' 2.5 `""$100,000" "Insurance""' 4  `""$5 Million" "Insurance""') ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf4a_insurance_levels_nosub.pdf", replace  

/*------------------  Panel B: Full Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_ins = 100 * hire_ins 

local row=1
mat R=J(10,5,.)

* Estimates by insurance cap level
reg hire_ins ibn.ins_cap i.subsidy_rate, cluster(firm_id) noconstant
foreach X in 5000.ins_cap 100000.ins_cap 5000000.ins_cap {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=1
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-values by insurance cap level relative to $5000 insurance cap result
local row=2
foreach X in 100000.ins_cap 5000000.ins_cap {
    preserve
    reg hire_ins ib5000.ins_cap i.subsidy_rate, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p =2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "$5,000 Insurance" 2 "$100,00 Insurance" 3 "$5 Million Insurance"
label values R4 R4

* Add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  `""$5,000" "Insurance""' 2.5 `""$100,000" "Insurance""' 4  `""$5 Million" "Insurance""') ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 

graph display, ysize(5) xsize(6)	
graph export "$Figures/xf4b_insurance_levels_full.pdf", replace

/*===========================================================================
Appendix Figure B.5: Job History Screening Levels
=============================================================================*/

/*------------------  Panel A: No Subsidy Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_hist = 100 * hire_hist 

local row=1
mat R=J(10,5,.)

* Estimates by number of past completed jobs
reg hire_hist ibn.past_jobs if subsidy_rate == 0, cluster(firm_id) noconstant
foreach X in 1.past_jobs 5.past_jobs 25.past_jobs {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=1
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-values by number of past completed jobs vs. 1 past completed job
local row=2
foreach X in 5.past_jobs 25.past_jobs {
    preserve
    reg hire_hist ib1.past_jobs  if subsidy_rate == 0, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p = 2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "1 Past Job" 2 "5 Past Jobs" 3 "25 Past Jobs"
label values R4 R4

*add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  "1 Past Job" 2.5 "5 Past Jobs" 4  "25 Past Jobs") ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf5a_performance_history_nosub.pdf", replace  

/*------------------  Panel B: Full Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_hist = 100 * hire_hist

local row=1
mat R=J(10,5,.)

* Estimates by number of past completed jobs
reg hire_hist i.subsidy_rate ibn.past_jobs, cluster(firm_id) noconstant
foreach X in 1.past_jobs 5.past_jobs 25.past_jobs {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    mat R[`row',5]=1
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}

* p-values by number of past completed jobs vs. 1 past completed job
local row=2
foreach X in 5.past_jobs 25.past_jobs {
    preserve
    reg hire_hist ib1.past_jobs i.subsidy_rate, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p = 2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "1 Past Job" 2 "5 Past Jobs" 3 "25 Past Jobs"
label values R4 R4


* Add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  "1 Past Job" 2.5 "5 Past Jobs" 4  "25 Past Jobs") ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf5b_performance_history_full.pdf", replace  

/*===========================================================================
Appendix Figure B.6: Limited Criminal Record Screening
=============================================================================*/

/*------------------  Panel A: No Subsidy Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_clean = 100 * hire_clean 

local row=1
mat R=J(10,5,.)

* Estimates by numbers of years since last arrest or conviction
reg hire_clean ibn.years_elapsed if subsidy_rate == 0, cluster(firm_id) noconstant coefl
foreach X in 1.years_elapsed 3.years_elapsed 7.years_elapsed {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format 
    local ++row
}	

* p-values by number of years since last arrest or conviction vs. 1 year since last arrest or conviction
local row=2
foreach X in 3.years_elapsed 7.years_elapsed {
    preserve
    reg hire_clean ib1.years_elapsed if subsidy_rate == 0, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p = 2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "1 Year Clean" 2 "3 Years Clean" 3 "7 Years Clean"
label values R4 R4


* Add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  "1 Year Clean" 2.5 "3 Years Clean" 4 "7 Years Clean") ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf6a_record_screening_nosub.pdf", replace 

/*------------------  Panel B: Full Sample ------------------*/
use "$Data/main_survey_long.dta", clear

* Normalize outcome variable
replace hire_clean = 100 * hire_clean 

local row=1
mat R=J(10,5,.)

* Estimates by number of years since last arrest or conviction
reg hire_clean i.subsidy_rate ibn.years_elapsed, cluster(firm_id) noconstant coefl
foreach X in 1.years_elapsed 3.years_elapsed 7.years_elapsed {
    mat R[`row',1]=_b[`X']
    mat R[`row',2]=_b[`X']-1.96*_se[`X']
    mat R[`row',3]=_b[`X']+1.96*_se[`X']
    mat R[`row',4]=`row'
    local coef_`row' = string(_b[`X'], "%8.0f")
    format
    local ++row
}	

* p-values by number of years since last arrest or conviction vs. 1 year since last arrest or conviction
local row=2
foreach X in 3.years_elapsed 7.years_elapsed {
    preserve
    reg hire_clean i.subsidy_rate ib1.years_elapsed, cluster(firm_id)
    local t = _b[`X']/_se[`X']
    local p = 2*ttail(e(df_r),abs(`t'))
    local p_value_`row' = substr(string(`p', "%8.2f"), 2, .)
    restore
    local ++row
}

clear	
svmat R
la var R1 "Willingness to Work with WCs (%)"
label define R4 1 "1 Year Clean" 2 "3 Years Clean" 3 "7 Years Clean"
label values R4 R4

* Add x axis spacing between groups
generate x_value = R4 if R4 == 1
replace x_value = R4 + 0.5 if R4 == 2
replace x_value = R4 + 1 if R4 == 3
sort x_value 
list x_value R4, sepby(R4)

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1.0  "1 Year Clean" 2.5 "3 Years Clean" 4  "7 Years Clean") ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2.5 "`coef_2'%") text(20 4 "`coef_3'%")  /// 
text(95 2.5 "{it:p}-value=`p_value_2'") text(95 4 "{it:p}-value=`p_value_3'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 


graph display, ysize(5) xsize(6)	
graph export "$Figures/xf6b_record_screening_full.pdf", replace 


/*===========================================================================
Appendix Figure B.7: High- and Low-Performance Information Effects (IV)
=============================================================================*/

* Set new scheme for High- and Low- Performance graphs
set scheme aurora

grstyle init platform, replace 
grstyle color p "77 45 82"
grstyle color p1 "139 149 86"
grstyle color p2 "25 114 120"
grstyle color p3 "40 61 59"
grstyle graphsize x 16
grstyle graphsize y 9
grstyle clockdir subtitle_position 11
grstyle gridringstyle subtitle_ring 0
grstyle set legend 7, inside nobox
grstyle gsize text medium        
grstyle gsize key_label medium    
grstyle gsize label medium    

* Turn off grid lines
grstyle yesno draw_major_hgrid       no
grstyle yesno draw_major_vgrid       no


gr_setscheme, scheme(platform)

local slide_aspect xsize(17) ysize(9)
local paper_aspect xsize(12) ysize(8) 

/*----------  Panel A: Belief Distribution, Low-Performance ----------*/
* Keep low-performance information treated respondents
local i = 2
use "$Data/main_survey_wide.dta", clear
keep if info_type == `i'

if `i' == 1 local highlight "maroon"
if `i' == 2 local highlight "navy"

* Get first stage of instrumental variable regression to derive effect of low-performance information treatment
gen info_gap = info_randomization*perception_gap
ivreg2  hire_sub_posterior (ln_posterior = info_gap) ln_prior hire_sub, first cluster(firm_id)
store_estimates ln_posterior
local b `r(beta)'`r(star)'
local se `r(se)'

* Calculate residuals
residualize perception_gap hire_sub, replace 
residualize hire_sub_posterior hire_sub, replace 
residualize info_gap hire_sub, replace 
residualize perception_update hire_sub, replace 

* Bin the results
egen perception_bins1 = cut(perception_gap) if info_randomization == 0, group(10)
egen perception_bins2 = cut(perception_gap) if info_randomization == 1, group(10)
gen perception_bins = perception_bins1 if info_randomization == 0
replace perception_bins = perception_bins2 if info_randomization == 1
bysort info_randomization perception_bins: egen binned_post = mean(perception_update)
bysort info_randomization perception_bins: egen binned_gap = mean(perception_gap)
preserve 
drop if missing(binned_gap)
sort binned_gap
local min_x = binned_gap[1]
local max_x = binned_gap[_N]
restore

* Graph first stage 
twoway (scatter binned_post binned_gap if info_randomization == 0, mcolor(gs6)) /// 
(scatter binned_post binned_gap if info_randomization == 1, mcolor(`highlight') msymbol(O)) /// 
(lfit perception_update perception_gap if info_randomization == 1 & perception_gap > `min_x' & perception_gap < `max_x', lcolor(`highlight')), ///
name(iv1,replace) legend(position(5) ring(0) order(2 "Shown Information." 1 "Not Shown Information.") size(large) bmargin(t+2)) /// 
xtitle("Perception Gap: Signal - Prior (Log)", size(large)) ytitle("Perception Update: Posterior - Prior (Log)" " ", size(large)) `paper_aspect' /// 
xlabel(,labsize(large)) ylabel(,labsize(large) format("%03.2f"))

graph export "$Figures/xf7a_perceptionupdatelowperf.pdf", replace  


/*----------  Panel B: Belief Distribution, High-Performance ----------*/
use "$Data/main_survey_wide.dta", clear

* Keep high-performance information treated respondents
local i = 1
keep if info_type == `i'
if `i' == 1 local highlight "maroon"
if `i' == 2 local highlight "navy"

* Get first stage of instrumental variable regression to derive effect of high-performance information treatment
gen info_gap = info_randomization*perception_gap
ivreg2  hire_sub_posterior (ln_posterior = info_gap) ln_prior hire_sub, first cluster(firm_id)
store_estimates ln_posterior
local b `r(beta)'`r(star)'
local se `r(se)'

* Calculate the residuals
residualize perception_gap hire_sub, replace 
residualize hire_sub_posterior hire_sub, replace 
residualize info_gap hire_sub, replace 
residualize perception_update hire_sub, replace 

* Bin the results
egen perception_bins1 = cut(perception_gap) if info_randomization == 0, group(10)
egen perception_bins2 = cut(perception_gap) if info_randomization == 1, group(10)
gen perception_bins = perception_bins1 if info_randomization == 0
replace perception_bins = perception_bins2 if info_randomization == 1
bysort info_randomization perception_bins: egen binned_post = mean(perception_update)
bysort info_randomization perception_bins: egen binned_gap = mean(perception_gap)
preserve 
drop if missing(binned_gap)
sort binned_gap
local min_x = binned_gap[1]
local max_x = binned_gap[_N]
restore

* Graph first stage 
twoway (scatter binned_post binned_gap if info_randomization == 0, mcolor(gs6)) /// 
(scatter binned_post binned_gap if info_randomization == 1, mcolor(`highlight') msymbol(O)) /// 
(lfit perception_update perception_gap if info_randomization == 1 & perception_gap > `min_x' & perception_gap < `max_x', lcolor(`highlight')), ///
name(iv1,replace) legend(position(5) ring(0) order(2 "Shown Information." 1 "Not Shown Information.") size(large) bmargin(t+2)) /// 
xtitle("Perception Gap: Signal - Prior (Log)", size(large)) ytitle("Perception Update: Posterior - Prior (Log)" " ", size(large)) `paper_aspect' /// 
xlabel(,labsize(large)) ylabel(-0.10(0.1)0.40,labsize(large) format("%03.2f"))

graph export "$Figures/xf7b_perceptionupdatehighperf.pdf", replace  


/*----------  Panel C: Belief Distribution IV Estimates, Low-Performance ----------*/
use "$Data/main_survey_wide.dta", clear

* Keep low-performance information treated respondents
local i = 2
keep if info_type == `i'

if `i' == 1 local highlight "maroon"
if `i' == 2 local highlight "navy"

* Get first stage results of instrumental variable regression to derive effect of low-performance information treatment
gen info_gap = info_randomization*perception_gap
ivreg2 hire_sub_posterior (ln_posterior = info_gap) ln_prior hire_sub, first cluster(firm_id)
store_estimates ln_posterior
local b `r(beta)'`r(star)'
local se `r(se)'

* Calculate residuals
residualize perception_gap hire_sub, replace 
residualize hire_sub_posterior hire_sub, replace 
residualize info_gap hire_sub, replace 
residualize perception_update hire_sub, replace 

* Bin the results
egen perception_bins1 = cut(perception_gap) if info_randomization == 0, group(10)
egen perception_bins2 = cut(perception_gap) if info_randomization == 1, group(10)
gen perception_bins = perception_bins1 if info_randomization == 0
replace perception_bins = perception_bins2 if info_randomization == 1
bysort info_randomization perception_bins: egen binned_post = mean(perception_update)
bysort info_randomization perception_bins: egen binned_gap = mean(perception_gap)
preserve 
drop if missing(binned_gap)
sort binned_gap
local min_x = binned_gap[1]
local max_x = binned_gap[_N]
restore

* Calculate second stage results
use "$Data/main_survey_wide.dta", clear
keep if info_type == `i'
gen info_gap = info_randomization*perception_gap
reg ln_posterior hire_sub_posterior perception_gap hire_sub info_gap

* Calculate residuals
residualize ln_posterior ln_prior hire_sub, replace 
residualize info_gap ln_prior hire_sub, replace 
residualize hire_sub_posterior ln_prior hire_sub, replace 

reg ln_posterior info_gap
predict projected_posterior

* Bin the results
egen bins1 = cut(projected_posterior) if info_randomization == 0, group(10)
egen bins2 = cut(projected_posterior) if info_randomization == 1, group(10)
gen bins = bins1 if info_randomization == 0
replace bins = bins2 if info_randomization == 1
bysort info_randomization bins: egen binned_projection = mean(projected_posterior)
bysort info_randomization bins: egen binned_hire = mean(hire_sub_posterior)
preserve 
drop if missing(binned_projection)
sort binned_projection
local min_x = binned_projection[1]
local max_x = binned_projection[_N]
restore


* Graph second stage 
twoway (scatter binned_hire binned_projection if info_randomization == 0, mcolor(gs6)) /// 
(scatter binned_hire binned_projection if info_randomization == 1, mcolor(`highlight') msymbol(O)) /// 
(lfit hire_sub_posterior projected_posterior if info_randomization == 1 & projected_posterior > `min_x' & projected_posterior < `max_x', lcolor(`highlight')), ///
name(iv2,replace)  xtitle("Instrumented Posterior Beliefs (Log)", size(large)) ytitle("Posterior Demand" " ", size(large)) /// 
legend(position(5) ring(0) order(2 "Shown Information." 1 "Not Shown Information." ) size(large) bmargin(t+2)) `paper_aspect'   ///
xlabel(,labsize(large)) ylabel(,labsize(large) format("%03.2f"))


graph export "$Figures/xf7c_ivestimateslowperf.pdf", replace as(pdf)


/*----------  Panel D: Belief Distribution IV Estimates, High-Performance ----------*/
use "$Data/main_survey_wide.dta", clear

* Keep high-performance information treated respondents
local i = 1
keep if info_type == `i'

if `i' == 1 local highlight "maroon"
if `i' == 2 local highlight "navy"

* Calcualte first-stage results
gen info_gap = info_randomization*perception_gap
ivreg2 hire_sub_posterior (ln_posterior = info_gap) ln_prior hire_sub, first cluster(firm_id)
store_estimates ln_posterior
local b `r(beta)'`r(star)'
local se `r(se)'

* Calculate residuals
residualize perception_gap hire_sub, replace 
residualize hire_sub_posterior hire_sub, replace 
residualize info_gap hire_sub, replace 
residualize perception_update hire_sub, replace 

* Bin the results
egen perception_bins1 = cut(perception_gap) if info_randomization == 0, group(10)
egen perception_bins2 = cut(perception_gap) if info_randomization == 1, group(10)
gen perception_bins = perception_bins1 if info_randomization == 0
replace perception_bins = perception_bins2 if info_randomization == 1
bysort info_randomization perception_bins: egen binned_post = mean(perception_update)
bysort info_randomization perception_bins: egen binned_gap = mean(perception_gap)
preserve 
drop if missing(binned_gap)
sort binned_gap
local min_x = binned_gap[1]
local max_x = binned_gap[_N]
restore

* Calculate second stage results
use "$Data/main_survey_wide.dta", clear
keep if info_type == `i'
gen info_gap = info_randomization*perception_gap
reg ln_posterior hire_sub_posterior perception_gap hire_sub info_gap

* Calculate residuals
residualize ln_posterior ln_prior hire_sub, replace 
residualize info_gap ln_prior hire_sub, replace 
residualize hire_sub_posterior ln_prior hire_sub, replace 

* Bin the results
reg ln_posterior info_gap
predict projected_posterior
egen bins1 = cut(projected_posterior) if info_randomization == 0, group(10)
egen bins2 = cut(projected_posterior) if info_randomization == 1, group(10)
gen bins = bins1 if info_randomization == 0
replace bins = bins2 if info_randomization == 1
bysort info_randomization bins: egen binned_projection = mean(projected_posterior)
bysort info_randomization bins: egen binned_hire = mean(hire_sub_posterior)
preserve 
drop if missing(binned_projection)
sort binned_projection
local min_x = binned_projection[1]
local max_x = binned_projection[_N]
restore


* Graph second stage 
twoway (scatter binned_hire binned_projection if info_randomization == 0, mcolor(gs6)) /// 
(scatter binned_hire binned_projection if info_randomization == 1, mcolor(`highlight') msymbol(O)) /// 
(lfit hire_sub_posterior projected_posterior if info_randomization == 1 & projected_posterior > `min_x' & projected_posterior < `max_x', lcolor(`highlight')), ///
name(iv2,replace)  xtitle("Instrumented Posterior Beliefs (Log)", size(large)) ytitle("Posterior Demand" " ", size(large)) `paper_aspect'   ///
legend(position(5) ring(0) order(2 "Shown Information." 1 "Not Shown Information.") size(large) bmargin(t+2)) graphregion(color(white)) bgcolor(white)    ///
xlabel(,labsize(large)) ylabel(,labsize(large) format("%03.2f"))

graph export "$Figures/xf7d_ivestimateshighperf.pdf", replace as(pdf)  

/*===========================================================================
Appendix Figure B.8: COVID-19 - Median
=============================================================================*/

/*------------------  Panel A: No Subsidy Sample ------------------*/

* Go back to black and white graph scheme
set scheme s2mono
grstyle init platform, replace 
grstyle clockdir subtitle_position 11
grstyle gridringstyle subtitle_ring 0
grstyle set legend 7, inside nobox
grstyle gsize text medium     
grstyle gsize text_option medium     
grstyle gsize filled_text medium     
grstyle gsize key_label medium    
grstyle gsize label medium    
* Turn off grid lines
grstyle yesno draw_major_hgrid       no
grstyle yesno draw_major_vgrid       no
gr_setscheme, scheme(platform)

use "$Data/main_survey_wide.dta", clear

* Normalize outcome variable
replace hire_sub = 100 * hire_sub 

local row=1
mat R=J(10,5,.)

* Estimate results for respondents before and after COVID-19 declaration of state of emergency 
gen SOE = state_of_emergency
reg hire_sub ibn.SOE if subsidy_rate == 0, nocons cluster(firm_id) coefl
foreach X in 0bn.SOE 1.SOE {
mat R[`row',1]=_b[`X']
mat R[`row',2]=_b[`X']-1.96*_se[`X']
mat R[`row',3]=_b[`X']+1.96*_se[`X']
mat R[`row',4]=`row'	
local coef_`row' = string(_b[`X'], "%8.0f")
format 
local ++row
}

* p-value for respondents after state of emergency vs. before
preserve
reg hire_sub SOE if subsidy_rate == 0, cluster(firm_id)
local t = _b[SOE]/_se[SOE]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_1 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimate results for respondents in top quartile COVID-19 cases/capita vs lower quartiles
gen COVID = median_cases_pc // COVID == 1 if above median COVID-19 cases/capita
reg hire_sub ibn.COVID if subsidy_rate == 0, nocons cluster(firm_id) coefl
foreach X in 0bn.COVID 1.COVID {
mat R[`row',1]=_b[`X']
mat R[`row',2]=_b[`X']-1.96*_se[`X']
mat R[`row',3]=_b[`X']+1.96*_se[`X']
mat R[`row',4]=`row'	
local coef_`row' = string(_b[`X'], "%8.0f")
format 
local ++row
}

* p-value for top quartile COVID-19 cases/capita vs. lower quartiles
preserve
reg hire_sub COVID if subsidy_rate == 0, cluster(firm_id)
local t = _b[COVID]/_se[COVID]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_2 = substr(string(`p', "%8.2f"), 2, .)
restore

clear
svmat R
la var R1 "Willingness to Work with WCs (%)"

* Define group based on order of regressions and predictions above
gen Group = .
replace Group = 1 if R4 == 1 | R4 == 2
replace Group = 2 if R4 == 3 | R4 == 4
label define Group 1 "State of Emergency" 2 "County-Level COVID-19 Cases Per Capita"
label values Group Group

* Add x axis spacing between groups
generate x_value = R4 if Group == 1
replace x_value = R4 + 0.5 if Group == 2
replace x_value = R4 + 1 if Group == 3
sort x_value 
list x_value

rename R5 MeanEffect

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1 `""Before" "State of Emergency""' 2 `""After" "State of Emergency""' 3.5 `""Below Median" "COVID-19""' 4.5 `""Above Median" "COVID-19""') ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2 "`coef_2'%") text(20 3.5 "`coef_3'%")  text(16 4.5 "`coef_4'%") ///
xline(2.75, lpattern(-) lcolor(black)) ///
xline(5.25, lpattern(-) lcolor(black)) ///
text(95 1.5 "{it:p}-value=`p_value_1'") text(95 4 "{it:p}-value=`p_value_2'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 

graph display, ysize(5) xsize(9)
graph export "$Figures/xf8a_covid_nosub.pdf", replace 

/*------------------  Panel B: Full Sample ------------------*/
use "$Data/main_survey_wide.dta", clear

* Normalize outcome variable
replace hire_sub = 100 * hire_sub 

local row=1
mat R=J(10,5,.)

* Estimate results for respondents before and after COVID-19 declaration of state of emergency 
gen SOE = state_of_emergency
reg hire_sub ibn.SOE i.subsidy_rate, nocons cluster(firm_id) coefl
foreach X in 0bn.SOE 1.SOE {
mat R[`row',1]=_b[`X']
mat R[`row',2]=_b[`X']-1.96*_se[`X']
mat R[`row',3]=_b[`X']+1.96*_se[`X']
mat R[`row',4]=`row'	
local coef_`row' = string(_b[`X'], "%8.0f")
format 
local ++row
}

* p-value for respondents after state of emergency vs. before
preserve
reg hire_sub SOE i.subsidy_rate, cluster(firm_id)
local t = _b[SOE]/_se[SOE]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_1 = substr(string(`p', "%8.2f"), 2, .)
restore

* Estimate results for respondents in top quartile COVID-19 cases/capita vs lower quartiles
gen COVID = median_cases_pc // COVID == 1 if above median COVID-19 cases/capita
reg hire_sub ibn.COVID i.subsidy_rate, nocons cluster(firm_id) coefl
foreach X in 0bn.COVID 1.COVID {
mat R[`row',1]=_b[`X']
mat R[`row',2]=_b[`X']-1.96*_se[`X']
mat R[`row',3]=_b[`X']+1.96*_se[`X']
mat R[`row',4]=`row'	
local coef_`row' = string(_b[`X'], "%8.0f")
format 
local ++row
}

* p-value for top quartile COVID-19 cases/capita vs. lower quartiles
preserve
reg hire_sub COVID i.subsidy_rate, cluster(firm_id)
local t = _b[COVID]/_se[COVID]
local p = 2*ttail(e(df_r),abs(`t'))
local p_value_2 = substr(string(`p', "%8.2f"), 2, .)
restore

clear
svmat R
la var R1 "Willingness to Work with WCs (%)"

* Define group based on order of regressions and predictions above
gen Group = .
replace Group = 1 if R4 == 1 | R4 == 2
replace Group = 2 if R4 == 3 | R4 == 4
label define Group 1 "State of Emergency" 2 "County-Level COVID-19 Cases Per Capita"
label values Group Group

* Add x axis spacing between groups
generate x_value = R4 if Group == 1
replace x_value = R4 + 0.5 if Group == 2
replace x_value = R4 + 1 if Group == 3
sort x_value 
list x_value

rename R5 MeanEffect

* Generate graph
twoway (bar R1 x_value, barw(1) fi(inten10) lc(black) lw(medium) ) ///
(rcap R3 R2 x_value, lc(gs5) ///
legend(off) xlabel(1 `""Before" "State of Emergency""' 2 `""After" "State of Emergency""' 3.5 `""Below Median" "COVID-19""' 4.5 `""Above Median" "COVID-19""') ///
ylabel(0(020)100, nogrid angle(0)) yscale(range(1.0))  ///
text(20 1 "`coef_1'%") text(20 2 "`coef_2'%") text(20 3.5 "`coef_3'%")  text(20 4.5 "`coef_4'%") ///
xline(2.75, lpattern(-) lcolor(black)) ///
xline(5.25, lpattern(-) lcolor(black)) ///
text(95 1.5 "{it:p}-value=`p_value_1'") text(95 4 "{it:p}-value=`p_value_2'") ///
subtitle("",  margin(zero) size(medium) pos(11)) ///
graphregion(color(white)) bgcolor(white) ytitle("Willing to Work with WCs (%)") xtitle("") xscale(titlegap(*10)) yscale(titlegap(*5))) 

graph display, ysize(5) xsize(9)
graph export "$Figures/xf8b_covid_full.pdf", replace 

log close
exit
